package com.west_traveler.find_page_fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.west_traveler.entity.TempDS;
import com.west_traveler.entity.TempList;
import com.west_traveler.entity.entity_editor.SpecialList;
import com.west_traveler.entity.entity_find_second_hot_recommend.SecondHotRecommendAlbum;
import com.west_traveler.recyclerview_adapter.SecondAlbumRecycleViewAdapter;
import com.west_traveler.ximalayafm.PlayActivity;
import com.west_traveler.ximalayafm.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SecondAlbumFragment extends Fragment implements SecondAlbumRecycleViewAdapter.OnChildClickListener {
    private SecondHotRecommendAlbum.DataBean albumDataBean;
    private Intent intent;

    public static SecondAlbumFragment getInstance(Object obj) {
        SecondAlbumFragment fragment = new SecondAlbumFragment();
        Bundle bundle = new Bundle();
        if (obj instanceof SecondHotRecommendAlbum.DataBean) {
            bundle.putParcelable("albumDataBean", ((SecondHotRecommendAlbum.DataBean) obj));
        }
        fragment.setArguments(bundle);
        return fragment;
    }

    public SecondAlbumFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Bundle bundle = getArguments();

        albumDataBean = bundle.getParcelable("albumDataBean");
        return inflater.inflate(R.layout.fragment_second_album, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_album);
        TextView tv_CollectCount = (TextView) view.findViewById(R.id.tv_count_collect);
        tv_CollectCount.setText("共" + albumDataBean.getTracks().getTotalCount() + "集");
        //适配器
        SecondAlbumRecycleViewAdapter adapter = new SecondAlbumRecycleViewAdapter(getActivity(), albumDataBean.getTracks());
        recyclerView.setAdapter(adapter);
        adapter.setListener(this);
        intent = new Intent();
    }

    @Override
    public void onChildClick(int position, long id, SecondHotRecommendAlbum.DataBean.TracksBean data) {
        intent.setClass(getActivity(), PlayActivity.class);
//        intent.putExtra("title", data.getTitle());
//        intent.putExtra("playUrl32", data.getPlayUrl64());
//        intent.putExtra("coverLarge", data.getCoverLarge());
//        intent.putExtra("nickname", data.getNickname());
//        intent.putExtra("smallLogo", data.getSmallLogo());
//        intent.putExtra("startService",true);

        Bundle bundle = new Bundle();
        TempDS tempDS = new TempDS();
        intent.putExtra("index", position);
        tempDS.setSmallLogo(data.getList().get(0).getSmallLogo());
        bundle.putBoolean("startService", true);

        List<SecondHotRecommendAlbum.DataBean.TracksBean.ListBean> list = data.getList();

        List<TempList> tempLists = new ArrayList<>();
        for (SecondHotRecommendAlbum.DataBean.TracksBean.ListBean listBean: list) {
            TempList tempList = new TempList();
            tempList.setNickName(listBean.getNickname());
            tempList.setTitle(listBean.getTitle());
            tempList.setCoverLarge(listBean.getCoverLarge());
            tempLists.add(tempList);
        }
        tempDS.setList(tempLists);
        String[] playPathArray = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            playPathArray[i] = list.get(i).getPlayUrl32();
        }
        bundle.putStringArray("allPlaySource",playPathArray);
        bundle.putParcelable("ds", tempDS);
        intent.putExtra("playDataSource", bundle);


        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.down_in,android.R.anim.fade_out);
    }
}
